package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.StageProcessDao;
import com.xbongbong.paas.domain.entity.StageProcessEntity;
import com.xbongbong.paas.model.StageProcessModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 流程阶段
 * @author 李少杰
 * @version v1.0.0
 * @date 2021/11/17 14:02
 */
@Service("stageProcessModel")
public class StageProcessModelImpl implements StageProcessModel {

    @Resource
    private StageProcessDao stageProcessDao;

    @Override
    public Integer insert(StageProcessEntity entity) {
        return stageProcessDao.insert(entity);
    }

    @Override
    public void insertBatch(List<StageProcessEntity> stageProcessEntityList) {
        stageProcessDao.insertBatch(stageProcessEntityList);
    }

    @Override
    public void updateBatch(List<StageProcessEntity> stageProcessEntityList, String corpid) {
        stageProcessDao.updateBatch(stageProcessEntityList, corpid);
    }

    @Override
    public void updateBatchPriority(List<StageProcessEntity> stageProcessEntityList, String corpid) {
        stageProcessDao.updateBatchPriority(stageProcessEntityList, corpid);
    }

    @Override
    public void updateBatch4MirrorRollback(List<StageProcessEntity> stageProcessEntityList, String corpid) {
        stageProcessDao.updateBatch4MirrorRollback(stageProcessEntityList, corpid);
    }

    @Override
    public void deleteBatch(List<Long> stageProcessIdIn, String corpid) {
        stageProcessDao.deleteBatch(stageProcessIdIn, corpid);
    }

    @Override
    public Integer update(StageProcessEntity entity) throws XbbException {
        return stageProcessDao.update(entity);
    }

    @Override
    public List<StageProcessEntity> findEntitys(Map<String, Object> param) {
        return stageProcessDao.findEntitys(param);
    }

    @Override
    public StageProcessEntity getByKey(Long key, String corpid) {
        return stageProcessDao.getByKey(key, corpid);
    }

    @Override
    public StageProcessEntity getEnableStageProcess(String corpid, Long formId) {
        return stageProcessDao.getEnableStageProcess( corpid, formId);
    }

    @Override
    public List<StageProcessEntity> getEnableStageProcessList(String corpid, Long formId) {
        return stageProcessDao.getEnableStageProcessList(corpid, formId);
    }

    @Override
    public Boolean isEnableMultiStageProcessVersion(String corpid, Long formId) {
        return stageProcessDao.isEnableMultiStageProcessVersion(corpid, formId);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return stageProcessDao.getEntitysCount(param);
    }

    @Override
    public List<StageProcessEntity> findEntitysWithoutSub(Map<String, Object> param) {
        return stageProcessDao.findEntitys(param);
    }

    @Override
    public Integer delete(StageProcessEntity entity) {
        return stageProcessDao.deleteByKey(entity.getId(), entity.getCorpid());
    }

    @Override
    public Integer deleteByFormId(Long formId, String corpid) {
        return stageProcessDao.deleteByFormId(formId, corpid);
    }

    @Override
    public StageProcessEntity getByTransferVersion(String corpid, Long formId, Integer transferVersion) {
        return stageProcessDao.getByTransferVersion(corpid, formId, transferVersion);
    }

    @Override
    public StageProcessEntity getOldTransferVersionProcess(Long stageProcessId, String stageCode, String corpid, Long formId) throws XbbException {
        StageProcessEntity stageProcessEntity = null;
        if (Objects.isNull(stageProcessId) && StringUtil.isNotEmpty(stageCode)) {
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("corpid", corpid);
            map.put("formId", formId);
            map.put("oldTransferVersion", 1);
            List<StageProcessEntity> stageProcessList = findEntitys(map);
            if(CollectionUtils.isNotEmpty(stageProcessList)){
                stageProcessEntity = stageProcessList.get(0);
            }
        }
        return stageProcessEntity;
    }
}
